package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TBanner;
import com.quwan.manager.service.BannerSystemService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laozhang
 * @date 2021/9/25 11:47
 */
@Api(tags = "后台轮播banner管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/banner")
public class BannerSystemController extends BaseController {
    private final BannerSystemService bannerSystemService;

    @ApiOperation(value = "轮播banner列表分页",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("bannerListPage")
    public ApiResult<PageResult<TBanner>> bannerListPage(@RequestParam(defaultValue = "1") int page,
                                                        @RequestParam(defaultValue = "10") int size,
                                                       @RequestParam(required = false) String title) {
        PageResult<TBanner> res = bannerSystemService.getBannerListPage(page, size, title);
        return ApiResult.success(res);
    }

    @ApiOperation(value = "轮播banner列表",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("bannerList")
    public ApiResult<List<TBanner>> bannerList(String title) {
        List<TBanner> res = bannerSystemService.getBannerList(title);
        return ApiResult.success(res);
    }

    @ApiOperation("轮播banner修改——不需要传时间字段")
    @PostMapping("updateBanner")
    public ApiResult updateBanner(TBanner banner){
        banner.setUpdateTime(LocalDateTime.now());
        boolean success = bannerSystemService.updateBanner(banner);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addBanner")
    @ApiOperation("添加轮播banner——不需要传时间字段")
    public ApiResult addbanner(TBanner banner){
        banner.setUpdateTime(LocalDateTime.now());
        boolean success = bannerSystemService.addBanner(banner);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation(value = "轮播banner详情",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("detailBanner")
    public ApiResult detailBanner(@RequestParam(value = "id") Integer id){
        TBanner banner = bannerSystemService.detailBanner(id);
        return ApiResult.success(banner);

    }

    @PostMapping("delbanner")
    @ApiOperation("删除轮播banner")
    public ApiResult delbanner(@RequestParam(value = "id") Integer id){
        TBanner banner = new TBanner();
        banner.setId(Long.valueOf(id));
        banner.setIsDelete(1);
        boolean success = bannerSystemService.delBanner(banner);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("status")
    @ApiOperation("修改状态")
    public ApiResult status(@RequestParam(value = "id") Integer id,@RequestParam(value = "status") Integer status){
        TBanner banner = new TBanner();
        banner.setId(Long.valueOf(id));
        banner.setIsDelete(status);
        boolean success = bannerSystemService.changeStatus(banner);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }



}
